package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.PersonTypeSalary;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.IPersonTypeSalaryService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestSchema(schemaId = "PersonTypeSalaryController")
@RestController
@RequestMapping(value = "/PersonTypeSalaryController", produces = MediaType.APPLICATION_JSON)
public class PersonTypeSalaryController  extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonTypeSalaryController.class);

    @Autowired
    private IPersonTypeSalaryService personTypeSalaryService;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;



    @GetMapping("/queryPersonTypeSalaryByType")
    public ApiResultDTO queryPersonInfoPage(@RequestHeader("X-Token") String tokenid,
                                            @RequestParam(value = "personTypeCode", required = false) String personTypeCode) {
        try {
            List<String> personTypes=new ArrayList<>();
            personTypes.add(personTypeCode);
            UserDTO userDTO = getUser(tokenid);
            List<PersonTypeSalary> requestList=personTypeSalaryService.getInfoByPersonTypes(personTypes,userDTO);
            return ApiResultDTO.success(requestList,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
       }

    }

    @GetMapping("/getAllPersonType")
    public ApiResultDTO getAllPersonType(@RequestHeader("X-Token") String tokenid) {
        try {

            UserDTO userDTO = getUser(tokenid);
            List<Map<String,Object>> requestList=personTypeSalaryService.getAllPersonTypeByProvince(userDTO);
            return ApiResultDTO.success(requestList,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }

    }

    @PostMapping("/savePersonTypeSalary")
    public ApiResultDTO savePersonTypeSalary(@RequestHeader("X-Token") String tokenid,
                                             @RequestBody  List<PersonTypeSalary>  personTypeSalarys) {
        try {

            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资配置管理--工资项目挂接--保存");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);


            personTypeSalaryService.batchSavePersonTypeSalary(personTypeSalarys,userDTO);
            return ApiResultDTO.success("保存成功",null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存失败！");
        }

    }

    @GetMapping({"/getAllPersonTypeSalary"})
    public ApiResultDTO getAllPersonTypeSalary(@RequestHeader("X-Token") String tokenid) {
        try {
            UserDTO userDTO = this.getUser(tokenid);
            List<Map<String, Object>> requestList = this.personTypeSalaryService.getAllPersonTypeSalary(userDTO);
            return ApiResultDTO.success(requestList, (String)null);
        } catch (Exception var4) {
            logger.error(var4.getMessage(), var4);
            return ApiResultDTO.error("140999", "查询失败！");
        }
    }

}
